package com.example.hbsm_video.controller;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.crypto.SecureUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.example.hbsm_video.entity.Admin;
import com.example.hbsm_video.entity.Result;
import com.example.hbsm_video.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import util.JwtUtil;
import util.ThreadLocalUtil;

import java.util.HashMap;
import java.util.Map;


//管理员/审核员业务接口

@RequestMapping("/admin")
@RestController
public class AdminController {

    @Autowired
    private AdminService adminService;

    @GetMapping("/add")
    public boolean add(Admin admin){
        //根据名称查询管理员是否存在
        boolean b = adminService.exists(Wrappers.query(Admin.class).eq("username",admin.getUsername()));
        if(b){
            //用户已存在
            return false;
        }
        //实现对密码的加密
        //生成随机盐（token)
        String salt = RandomUtil.randomString(16);
        //将输入密码和盐组合
        String pwd = admin.getPassword() + salt;
        //对组合后的密码进行MD5加密
        pwd = SecureUtil.md5(pwd);
        //将加密后的密码和随机盐设置到对象中
        admin.setPassword(pwd);
        admin.setSalt(salt);
        //将数据持久化到数据库
        return adminService.save(admin);
    }

    @PostMapping("/login")
    public Result<String> login(String username, String password){
        // 1.
        Admin admin = adminService.getOne(Wrappers.query(Admin.class).eq("username",username));
        if(admin==null){
            return Result.error("用户名不存在");
        }
        else{
            String salt = admin.getSalt();
            String pwd = SecureUtil.md5(password + salt);
            if(pwd.equals(admin.getPassword())){
                // 生成token
                Map<String, Object> claims=new HashMap<>();
                claims.put("id",admin.getId());
                claims.put("name",admin.getUsername());

                String token = JwtUtil.getToken(claims);
                return Result.success(token);
            }
            else{
                return Result.error("密码错误");
            }
        }
    }

    @PostMapping("/updateAdmin")
    public Result<String> updateUser(Long id, String newPassword) {
        Admin admin = adminService.getById(id);
        if (admin == null) {
            return Result.error("用户不存在");
        }
        String salt = admin.getSalt();
        String pwd = SecureUtil.md5(newPassword + salt);
        admin.setPassword(pwd);
        adminService.updateById(admin);
        return Result.success("用户信息更新成功");
    }

    @PostMapping("/deleteAdmin")
    public Result<String> deleteUser(Long id) {
        boolean success = adminService.removeById(id);
        if (success) {
            return Result.success("用户删除成功");
        } else {
            return Result.error("用户删除失败");
        }
    }

    @GetMapping("/infoAdmin") //获取当前登录管理员信息
    public Result<Admin> info() {
        Map<String, Object> map = ThreadLocalUtil.get();
        Admin admin = adminService.getById((Integer) map.get("id"));
        if (admin == null) {
            return Result.error("获取失败");
        }
        return Result.success(admin);
    }
}
